public class Test {

    //String的初始化方式
    public static void main1(String[] args) {
        String str1 = "str1 = hello World";
        String str2 = new String("str2 = hello World");
        String str3 = new String();

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }


    //对于被双引号括起来的字符串，是被放在字符常量区的，所以当字符常量区有abc，那么就不会继续在往字符常量区存放abc，而是直接使用
    public static void main2(String[] args) {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);//输出true，因为他   们都指向同一个字符常量区的abc

    }

    public static void main3(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = new String("ABC");
        System.out.println(s1 == s2);//输出false，因为s1存放的是new出来对象的地址，s2也是存放new出来对象的地址
        //所以s1和s2不同，但是他么吧都是指向字符串常量区的abc

        //equals是比较俩个字符串是否相同
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));

        //compareTo是用俩个字符串s1 - s2，返回值是int类型
        System.out.println(s1.compareTo(s2));

        //compareToIgnoreCase是忽略大小比较
        System.out.println(s1.compareToIgnoreCase(s3));

    }


    //charAt --- 输出字符串某下标位置的字符
    public static void main4(String[] args) {
        String str = "xmcjh";
        char ch = str.charAt(2);//输出str下标2的字符
        System.out.println(ch);

        //通过for循环遍历整个str
        for(int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            System.out.print(ch + " ");
        }
    }

    //indexOf --- 查找某个字符或字符串起始位置
    public static void main5(String[] args) {
        String str = "xmcjh";
        int index = str.indexOf("j");
        System.out.println("j的起始下标为：" + index);

        index = str.indexOf("x", 1);//从下标位置1开始查找字符x，找不到返回-1
        System.out.println("x的起始下标为" + index);

    }

    //lastIndexOf --- 从后往前查找字符或字符串的起始位置
    public static void main6(String[] args) {
        String str = "xmcjh";
        int index = str.lastIndexOf("xm");
        System.out.println(index);
    }

    //valudeOf --- 将整数，浮点数转换成字符串
    public static void main7(String[] args) {
        int x = 1234567;
        String str = String.valueOf(x);
        System.out.println(str);

        double y = 100.86;
        str = String.valueOf(y);
        System.out.println(str);
    }

    //将字符串转换为整数或浮点数
    public static void main8(String[] args) {
        int x = Integer.parseInt("12");
        System.out.println(x + 100);

        double d = Double.parseDouble("100.86");
        System.out.println(d + 0.01);
    }

    //大小写转换 --- toUpperCase  toLowerCase
    public static void main9(String[] args) {
        String str  ="hello WORLD";
        String ret1 = str.toUpperCase();//转大写
        System.out.println(ret1);

        String ret2 = str.toLowerCase();//转小写
        System.out.println(ret2);

        //注意，原来的str是不变的，转大小写都是new了新对象
        System.out.println(str);
    }


    //字符串和数组互相转换 --- toCharArray
    public static void main10(String[] args) {
        String str = "xmcjh";
        char[] array = str.toCharArray();
        for(char e : array) {
            System.out.print(e + " ");
        }

        System.out.println();

        //数组转字符串
        char[] arr = {'x', 'm', 'c', 'j', 'h'};
        String s = new String(arr);
        System.out.println(s);
    }

    //字符串的替换 --- replace
    public static void main11(String[] args) {
        String str = "xmcjhxmcjh";
        String ret = str.replace('m', '*');//把m字符替换成*
        System.out.println(ret);

        String ret1 = str.replace("mc", "******");//把mc替换成*****
        System.out.println(ret1);

        String ret2 = str.replaceFirst("xm", "***");//只替换第一次出现的xm
        System.out.println(ret2);

    }


    //字符串拆分 --- split
    public static void main12(String[] args) {
        String str = "hello world xm cjh";
        String[] ret1 = str.split(" ");//以空格拆分
        for(String e : ret1) {
            System.out.println(e);
        }

        System.out.println("*****************");

        String[] ret2 = str.split(" ", 2);//以空格拆分，最多分俩组
        for(String e : ret2) {
            System.out.println(e);
        }

        System.out.println("*****************");

        String s = "192.168.1.210";
        String[] ret3 = s.split("//.");//要特殊转义

    }

    //字符串截取 --- subString
    public static void main13(String[] args) {
        String str = "xmcjh";
        String ret = str.substring(2);//从下标2位置开始截取
        System.out.println(ret);

        String ret1 = str.substring(0,1);//截取[0,1)下标的字符串
        System.out.println(ret1);

        String s = "  hello world   ";
        System.out.println("[" + s + "]");
        System.out.println("[" + s.trim() + "]");//trim去除左右两边的空格
    }


    //StringBuilder --- 不会创建临时对象，都是直接在stringbuilder基础上直接添加新的字符串
    public static void main14(String[] args) {
        StringBuilder stringbuilder = new StringBuilder();
        stringbuilder.append("hello");
        System.out.println(stringbuilder);
        stringbuilder.append("world");
        System.out.println(stringbuilder);
    }

    //StringBuffer --- 和StringBuilder相比多了synchronized,当写入内容的时候会上锁，写完后才能解锁，然后才能继续写其他内容
    public static void main15(String[] args) {
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append("hello");
        System.out.println(stringbuffer);
        stringbuffer.append("world");
        System.out.println(stringbuffer);
    }

}
































